package com.xinghou.platform.aspect;

import com.xinghou.platform.entity.req.BaseReq;
import com.xinghou.platform.server.api.bussiness.UserInfoBusinessService;
import com.xinghou.platform.server.api.log.ApiLogger;
import com.xinghou.platform.server.api.log.LoggerFactory;
import com.xinghou.platform.util.SignUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import com.xinghou.platform.dao.bases.util.StringUtils;
import javax.annotation.Resource;

/**
 * Created by qingshengzheng on 16/7/16.
 */
@Aspect
@Component
public class ControllerAspect {
    private Logger serverLogger = LoggerFactory.getServerInfoLogger(ControllerAspect.class);
    private ApiLogger apiLogger=new ApiLogger();
    @Resource
    private UserInfoBusinessService userInfoBusinessService;
    @Pointcut("execution(public * com.xinghou.platform.server.api.controller..*.*(..))")
    public void apiinfo() {
    }
    @Around("apiinfo()&& @annotation(log)")
    public String apiinfo(ProceedingJoinPoint pjp,RequestMapping log) throws Throwable {

        Long a = System.currentTimeMillis();
        Object obj = ((Object[]) pjp.getArgs())[0];
        String requrl="";
        if (obj instanceof BaseReq) {
            SignUtil.checkRequestJson(obj);

             requrl=log.value()[0];
            BaseReq baseReq = (BaseReq) obj;
            if(null!= baseReq && !StringUtils.isNullOrEmpty(baseReq.getOperuid())){
                userInfoBusinessService.checkclosureByUserId(baseReq.getOperuid());
            }
            apiLogger.log(requrl, baseReq);
        }
        String v = (String) pjp.proceed();
        Long b = System.currentTimeMillis();
        serverLogger.info("RESPONSE:url={},rt=[--{}-- millsecond],resInfo={}",requrl,(b-a),v);
        return v;
    }
}
